<?php
// {{{ includes
load_class('Controller_BoBase');
load_class('GD_FCKeditor');
load_file('FLEA_Helper_Array');
// }}}

/**
 * 定义Controller_MBoContent
 *
 * @author jack
 * @todo 用户后台管理 
 */


class Controller_MBoContent extends Controller_BoBase {
	
	var $_modelContent;
	var $_ui;
	
	function Controller_MBoContent(){
		parent::Controller_BoBase();
		$this->_modelContent=get_singleton('Model_Content');
		$this->_ui=FLEA::initWebControls();
		
	}
	
	
	function actionIndex(){
		
		$conditions='';
		$page=(int)$_GET['page'];
		$pageSize=10;
		
		if($_GET['sortby'] && $_GET['sort']){
			$sort=$_GET['sortby'].' '.$_GET['sort'];
		}
		else{
			$sort='id desc';
		}

		$list = $this->_modelContent->getList($conditions,$page,$pageSize,$sort);
		 
		include(BACKEND_TPL_DIR.'/content/index.tpl.php');
	}
	
	
	function actionADD(){
		$this->_Content();
	}
	
	function actionEdit(){
		$id=(int)$_GET['id'];
		if(!$id) redirect($this->_controllerName);
		
		$rowset=$this->_modelContent->find($id);
		$this->_Content($rowset);
		
	}
	function actionSave(){
		//检查令牌，防止站外提交
		if(!$this->checkToken($_POST['token'])){
			$error['token']='Illegal submitted data!';
		}
		
		if($error){
			$this->_Content($_POST,$error);
		}
		else{
			$_POST['seq']=$_POST['seq'] == '' ? 0 : (int)$_POST['seq'];
			
			$_POST['addable'] = $_POST['addable']==''?'N':$_POST['addable'];
			$_POST['editable'] = $_POST['editable']==''?'N':$_POST['editable'];
			$_POST['removeable'] = $_POST['removeable']==''?'N':$_POST['removeable'];

			if($_POST['id']){
				$return=$this->_modelContent->update($_POST);
				$id=$_POST['id'];
				$message='Page Updated.';
			}
			else{
				$return=$this->_modelContent->create($_POST);
				$id=$return;
				$message='New Page Created.';
			}
			
			 
						
			if($return){
				
				$url=url($this->_controllerName,'');
				goto($url,3,$message);
			}
			else {
				$message='System error, please try again later.';
				$url=url($this->_controllerName,'');
				goto($url,3,$message);
			}
			
		}
		
		 
	}
	
	function _Content($rowset='',$error=''){
		
		$action=url($this->_controllerName,'Save');
		$token=$this->getToken();
		
		$Editor=&new GD_FCKeditor('content');
  		$Editor->Height=600;
  		$Editor->Value=$rowset['content'];
		
		include(BACKEND_TPL_DIR.'/content/content.tpl.php');
	}
	
	function actionRemove(){
		$id=(int)$_GET['id'];
		
		if($id) {
			$this->_Remove($id);
			$message = 'The Content  has been removed.';
			$url=url($this->_controllerName);
			goto($url,3,$message);
		}
		else redirect(url($this->_controllerName));
	}
	
	function actionRemoveSelected(){
		if($_POST['id']){
			foreach ($_POST['id'] as $id){
				$this->_Remove($id);
			}
		}
		
		$url=url($this->_controllerName,'');
		$message='The Pages have been deleted.';
		goto($url,3,$message);
	}
	
	function _Remove($id){
		
		$Content=$this->_modelContent->find($id);
		
		if($Content){
			$file=APP_DIR.DS.get_app_inf('ContentFiles').DS.$Content['photo'];
			@unlink($file);
			
			return $this->_modelContent->removeByPkv($id);
			
			
		}
		else {
			return false;
		}
		
		
	}
	 
}



